import React, {useState, useRef, useEffect} from 'react'
import {PageHeaderWrapper} from '@ant-design/pro-layout'
import ProTable, {ProColumns, ActionType} from '@ant-design/pro-table'
import {Link, connect} from 'umi'
import {getRecordList} from '../service'
import {newContractTypeStatusList,endTypeList, genderList, signRemarkList,employeeStatusList} from "@/utils/constant"
import moment from 'moment'
import {Button,DatePicker} from 'antd'

const TableList = () => {
  const actionRef = useRef();
  const [time,setTime] = useState(null);
  const [params,setParams] = useState({});
  const handleContractExport = () => {
    window.open('/hr-ssc/api/api-admin/export/exportContractRecord?contractEndDate='+ time);
  };

  const getTime = (date,dateStr) =>{
    setTime(dateStr);
    setParams({contractEndDate:dateStr});
  };

  const columns = [
    {
      title: '工号',
      dataIndex: 'code',
      hideInSearch: true,
      fixed: 'left',
      width:120
    },
    {
      title: '姓名',
      dataIndex: 'name',
      width:80,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
      fixed: 'left',
    },
    {
      title: '员工状态',
      dataIndex: 'employeestatus',
      hideInSearch: true,
      fixed: 'left',
      width:120,
      render: (text) =>{
        return employeeStatusList.find((item) => item.value === text)?.label;
      }
    },
    {
      title: '性别',
      dataIndex: 'gender',
      hideInSearch: true,
      render: (text) => {
        const res = genderList.find((item) => item.value === text.toString());
        return res ? res.label : text
      }
    },
    {
      title: '身份证号码',
      dataIndex: 'idcard',
      hideInSearch: true,
    },
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      hideInSearch: true,
      render: (text) => {
        return text !== '-' ? moment(text).format('YYYY-MM-DD') : ''
      },
    },
    {
      title: '公司',
      dataIndex: 'orgName',
    },
    {
      title: '部门',
      dataIndex: 'unitName'
    },
    {
      title: '岗位',
      dataIndex: 'jobName',
      hideInSearch: true,
    },
    {
      title: '签署原因',
      dataIndex: 'operateType',
      hideInSearch: true,
      render: (text) => {
        const res = signRemarkList.find((item) => item.value === parseInt(text));
        return res ? res.label : text
      },
    },
    {
      title: '签订合同时所在公司',
      dataIndex: 'beginOrgName',
      hideInSearch: true,
    },
    {
      title: '签订合同时所在部门',
      dataIndex: 'beginUnitName',
      hideInSearch: true,
    },
    {
      title: '签订合同时岗位',
      dataIndex: 'beginJobName',
      hideInSearch: true,
    },
    {
      title: '合同类型',
      dataIndex: 'beginContractType',
      valueEnum: {
        1: { text: newContractTypeStatusList[0].label },
        2: { text: newContractTypeStatusList[2].label },
        3: { text: newContractTypeStatusList[1].label },
      },
    },
    {
      title: '合同生效日',
      dataIndex: 'contractBeginDate',
      hideInSearch: true,
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
    },
    {
      title: '合同预计终止日期',
      dataIndex: 'contractEndDate',
      hideInSearch: true,
      render: (text) => {
        return text !== '-' ? moment(text).format('YYYY-MM-DD') : ''
      },
    },
    {
      title: '甲方主体',
      dataIndex: 'employerCompany',
      hideInSearch: true,
    },
    {
      title: '合同期限类型',
      dataIndex: 'contractTimeType',
      hideInSearch: true,
      render: (text) => {
        return text !== 6 ? '固定期限合同': '无固定期限合同'
      },
    },
    {
      title: '合同状态',
      dataIndex: 'status',
      valueEnum: {
        0: { text: '未生效', status: 'Default' },
        1: { text: '已生效', status: 'Success' },
        2: { text: '已失效', status: 'Default' },
        3: { text: '已过期', status: 'Default' },
      },
    },
    {
      title: '合同实际终止日期',
      dataIndex: 'endTime',
      hideInSearch: true,
      render: (text) => {
        return text !== '-'  ? moment(text).format('YYYY-MM-DD') : ''
      },
    },
    {
      title: '解除合同签订日期',
      dataIndex: 'endSignDate',
      hideInSearch: true,
      render: (text) => {
        return text !== '-'  ? moment(text).format('YYYY-MM-DD') : ''
      },
    },
    {
      title: '解除合同签订地址',
      dataIndex: 'endContractUrl',
      hideInSearch: true,
      onCell: ()=>{
        return {
          style:{
            maxWidth:200,
            overflow:'hidden',
            whiteSpace:'nowrap',
            textOverflow:'ellipsis',
            cursor:'pointer',
          }
        }
      },
      render: (text) => {
        return <a href={text} target="_blank">{text}</a>
      }
    },
    {
      title: '解除类型',
      dataIndex: 'endType',
      hideInTable: true,
      valueEnum: {
        1: { text: '合同解除'},
        2: { text: '终止解除'},
        7: { text: '变更'},
      },
    },
    {
      title: '解除类型',
      dataIndex: 'endType',
      hideInSearch: true,
      render: (text) => {
        const res = endTypeList.find((item) => item.value === parseInt(text));
        return res ? res.label : text
      },
    },
    {
      title: '查看合同',
      dataIndex: 'beginContractUrl',
      hideInSearch: true,
      onCell: ()=>{
        return {
          style:{
            maxWidth:200,
            overflow:'hidden',
            whiteSpace:'nowrap',
            textOverflow:'ellipsis',
            cursor:'pointer',
          }
        }
      },
      render: (text) => {
        return <a href={text} target="_blank">{text}</a>
      }
    },
    {
      title: '预计终止日期',
      dataIndex: 'contractEndDate',
      renderFormItem: (item, props) => {
        return <DatePicker {...props} onChange={getTime} className='widthStyle'/>;
      },
      hideInTable: true,
    },
  ];

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        params={params}
        request={(params) => getRecordList(params)}
        columns={columns}
        scroll={{x: '220%'}}
        toolBarRender={(action, { selectedRows }) => [
          <Button type="primary" onClick={handleContractExport}>导出合同</Button>
        ]}
      />
    </PageHeaderWrapper>
  )
}

export default connect(() => ({}))(TableList)
